package com.xuexiang.xqrcode.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import com.xuexiang.xqrcode.R;
import com.xuexiang.xqrcode.XQRCode;
import com.xuexiang.xqrcode.ui.CaptureFragment;
import com.xuexiang.xqrcode.util.QRCodeAnalyzeUtils;

/* loaded from: classes2.dex */
public class CaptureActivity extends AppCompatActivity {
    public static final String KEY_CAPTURE_THEME = "com.xuexiang.xqrcode.ui.KEY_CAPTURE_THEME";
    public static final int REQUEST_CODE_REQUEST_PERMISSIONS = 222;
    CaptureFragment.CameraInitCallBack cameraInitCallBack = new CaptureFragment.CameraInitCallBack() { // from class: com.xuexiang.xqrcode.ui.CaptureActivity.1
        @Override // com.xuexiang.xqrcode.ui.CaptureFragment.CameraInitCallBack
        public void callBack(Exception exc) {
            if (exc != null) {
                CaptureActivity.showNoPermissionTip(CaptureActivity.this);
                CaptureActivity.this.onCameraInitFailed();
                return;
            }
            CaptureActivity.this.onCameraInitSuccess();
        }
    };
    QRCodeAnalyzeUtils.AnalyzeCallback analyzeCallback = new QRCodeAnalyzeUtils.AnalyzeCallback() { // from class: com.xuexiang.xqrcode.ui.CaptureActivity.3
        @Override // com.xuexiang.xqrcode.util.QRCodeAnalyzeUtils.AnalyzeCallback
        public void onAnalyzeSuccess(Bitmap bitmap, String str) {
            CaptureActivity.this.handleAnalyzeSuccess(bitmap, str);
        }

        @Override // com.xuexiang.xqrcode.util.QRCodeAnalyzeUtils.AnalyzeCallback
        public void onAnalyzeFailed() {
            CaptureActivity.this.handleAnalyzeFailed();
        }
    };

    protected void beforeCapture() {
    }

    protected void onCameraInitFailed() {
    }

    protected void onCameraInitSuccess() {
    }

    public static void start(Fragment fragment, int i, int i2) {
        Intent intent = new Intent(fragment.getContext(), CaptureActivity.class);
        intent.putExtra(KEY_CAPTURE_THEME, i2);
        fragment.startActivityForResult(intent, i);
    }

    public static void start(Activity activity, int i, int i2) {
        Intent intent = new Intent(activity, CaptureActivity.class);
        intent.putExtra(KEY_CAPTURE_THEME, i2);
        activity.startActivityForResult(intent, i);
    }

    @Override // android.support.v7.app.AppCompatActivity, android.support.v4.app.FragmentActivity, android.support.v4.app.SupportActivity, android.app.Activity
    protected void onCreate(Bundle bundle) {
        setTheme(getIntent().getIntExtra(KEY_CAPTURE_THEME, R.style.XQRCodeTheme));
        super.onCreate(bundle);
        setContentView(getCaptureLayoutId());
        beforeCapture();
        if (Build.VERSION.SDK_INT >= 23 && ActivityCompat.checkSelfPermission(this, "android.permission.CAMERA") != 0) {
            requestPermissions(new String[]{"android.permission.CAMERA"}, 222);
        } else {
            initCaptureFragment();
        }
    }

    protected int getCaptureLayoutId() {
        return R.layout.xqrcode_activity_capture;
    }

    protected void initCaptureFragment() {
        CaptureFragment captureFragment = new CaptureFragment();
        captureFragment.setAnalyzeCallback(this.analyzeCallback);
        captureFragment.setCameraInitCallBack(this.cameraInitCallBack);
        getSupportFragmentManager().beginTransaction().replace(R.id.fl_zxing_container, captureFragment).commit();
    }

    public static AlertDialog showNoPermissionTip(Activity activity, DialogInterface.OnClickListener onClickListener) {
        return new AlertDialog.Builder(activity).setTitle(R.string.xqrcode_pay_attention).setMessage(R.string.xqrcode_not_get_permission).setPositiveButton(R.string.xqrcode_submit, onClickListener).show();
    }

    public static AlertDialog showNoPermissionTip(final Activity activity) {
        return showNoPermissionTip(activity, new DialogInterface.OnClickListener() { // from class: com.xuexiang.xqrcode.ui.CaptureActivity.2
            @Override // android.content.DialogInterface.OnClickListener
            public void onClick(DialogInterface dialogInterface, int i) {
                activity.finish();
            }
        });
    }

    protected void handleAnalyzeSuccess(Bitmap bitmap, String str) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putInt(XQRCode.RESULT_TYPE, 1);
        bundle.putString(XQRCode.RESULT_DATA, str);
        intent.putExtras(bundle);
        setResult(-1, intent);
        finish();
    }

    protected void handleAnalyzeFailed() {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putInt(XQRCode.RESULT_TYPE, 2);
        bundle.putString(XQRCode.RESULT_DATA, "");
        intent.putExtras(bundle);
        setResult(-1, intent);
        finish();
    }

    @Override // android.support.v4.app.FragmentActivity, android.app.Activity, android.support.v4.app.ActivityCompat.OnRequestPermissionsResultCallback
    public void onRequestPermissionsResult(int i, String[] strArr, int[] iArr) {
        super.onRequestPermissionsResult(i, strArr, iArr);
        if (i == 222) {
            if (iArr.length > 0 && iArr[0] == 0) {
                initCaptureFragment();
            } else {
                handleRequestPermissionDeny();
            }
        }
    }

    protected void handleRequestPermissionDeny() {
        showNoPermissionTip(this);
    }
}
